﻿using DbControl;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CentralMonitoring
{
    /// <summary>
    /// 设备终端配置窗体
    /// </summary>
    public partial class OilConfigEdit : Form
    {    
        public int m_Id = -1;
        private devices m_devices;
        public OilConfigEdit()
        {
            InitializeComponent();
        }
        private void InitCombox()
        {
            DataTable dt = OprDeviceConfig.GetConfigOils();
            if (dt != null)
            {
                DataRow row = dt.NewRow();
                row["id"] = -1;
                row["oil_code"] = "请选择";
                row["oil_name"] = "请选择";
                dt.Rows.InsertAt(row,0);                
                comboBoxOil.DisplayMember = "oil_name";
                comboBoxOil.ValueMember = "id";
                comboBoxOil.DataSource = dt;
                if (m_devices != null && m_devices.oil_id.HasValue)
                {
                    oils oil = OprOil.GetOilById(m_devices.oil_id.Value);
                    comboBoxOil.Text = oil.oil_name;
                }
                else
                {
                    comboBoxOil.SelectedIndex = 0;
                }
            }
        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            int oilId =(int)comboBoxOil.SelectedValue;
            if (oilId > 0)
            {             
                if (OprDeviceConfig.SetOil(m_Id, oilId))
                {

                    MessageBox.Show("油品设置成功！", "提示", MessageBoxButtons.OK);
                    DialogResult = DialogResult.OK;
                }
            }
            else
            {
                if (OprDeviceConfig.ClearOil(m_Id))
                {

                    MessageBox.Show("油品解除关联！", "提示", MessageBoxButtons.OK);
                    DialogResult = DialogResult.OK;
                }
            }
           
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DeviceConfigEdit_Load(object sender, EventArgs e)
        {
            if (m_Id > 0)
            {
                m_devices = OprDevice.GetdevicesById(m_Id);
                if (m_devices != null)
                {
                    
                    textBoxCode.Text = m_devices.device_code;
                    textBoxName.Text = m_devices.device_name;                   
                    textBoxSite.Text = m_devices.device_position;    
                }
            }
            InitCombox();
        }
    }
}
